﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EventTest
{
    public delegate void SpaceKeyPressedEventHandler();

    public class MyApp
    {
        public event SpaceKeyPressedEventHandler SpaceKeyPressed;

        protected virtual void OnSpaceKeyPressed()
        {
            //if(this.SpaceKeyPressed != null)
            //{
            //    SpaceKeyPressed();
            //}
            this.SpaceKeyPressed?.Invoke();
        }
        public void StartRun()
        {
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                if (keyInfo.Key==ConsoleKey.Spacebar)
                {
                    OnSpaceKeyPressed();
                }
                if (keyInfo.Key==ConsoleKey.Escape)
                {
                    break;
                }
            }
        }

    }
    class Program
    {
        static void App_SpaceKeyPressed()
        {
            Console.WriteLine("{0}按下空格键", DateTime.Now.ToLongTimeString());
        }
        static void Main(string[] args)
        {
            MyApp app = new MyApp();
            app.SpaceKeyPressed += App_SpaceKeyPressed;
            app.StartRun();
        }

    }
}
